package com.easyskool.services.core;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;

import com.easyskool.composer.AdminComposer;
import com.easyskool.constants.CommonConstants;
import com.easyskool.constants.MessageConstants;
import com.easyskool.exception.ComposerException;
import com.easyskool.utilfunction.UtilFunctions;
import com.ivp.cloud.stratus.logger.LoggingUtil;
import com.sun.jersey.spi.container.servlet.PerSession;

@Path("/Admin")
@PerSession
public class AdminService implements Serializable {

	
	private static LoggingUtil 	logger 	= 	(LoggingUtil)LoggingUtil.getLogger(AdminService.class.getName());
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	AdminComposer composer = new AdminComposer();

	@Path("/insertAdmin")
	@POST
	@Produces("application/json")
	public Response insertAdmin(@QueryParam("user") String user,
			@QueryParam("fields") String insertValues,
			@Context HttpServletRequest request) throws Exception {
		String result = null;
		System.out.println("usegrgd : fvgfg" + user);
		result = composer.insertAdmin(user, insertValues);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/getAdmin")
	@POST
	@Produces("application/json")
	public Response getAdmin(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		String result = null;
		result = composer.getAdmin(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/updateAdmin")
	@POST
	@Produces("application/json")
	public Response updateAdmin(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause,
			@QueryParam("updateClause") String updateClause) throws Exception {
		String result = null;
		result = composer.updateAdmin(user, whereClause,
				updateClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/deleteAdmin")
	@POST
	@Produces("application/json")
	public Response deleteAdmin(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		String result = null;
		result = composer.deleteAdmin(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/deleteSelectedAdmin")
	@POST
	@Produces("application/json")
	public Response deleteSelectedAdmin(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause,
			@Context HttpServletRequest req) throws Exception {
		
		String result = null;
		HttpSession session= req.getSession(true);
		user = (String) session.getAttribute(CommonConstants.USERNAME);
		result = composer.deleteSelectedAdmin(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}
	
	@Path("/insertTeacher")
	@POST
	@Produces("application/json")
	public Response insertTeacher(@QueryParam("user") String user,
			@QueryParam("fields") String whereClause,
			@Context HttpServletRequest request) throws Exception {
		
		HttpSession session = request.getSession(false);
		user =	(String)session.getAttribute(CommonConstants.USERNAME);
		String result = null;
		result = composer.insertTeacher(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/getTeachers")
	@POST
	@Produces("application/json")
	public Response getTeachers(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		String result = null;
		result = composer.getTeachers(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/updateTeacher")
	@POST
	@Produces("application/json")
	public Response updateTeacher(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause,
			@QueryParam("updateClause") String updateClause,
			@Context HttpServletRequest request) throws Exception {
		
		HttpSession session = request.getSession(false);
		user =	(String)session.getAttribute(CommonConstants.USERNAME);
		String result = null;
		result = composer.updateTeacher(user, whereClause,
				updateClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/deleteTeacher")
	@POST
	@Produces("application/json")
	public Response deleteTeacher(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		try {
			composer.deleteTeacher(user, whereClause);
		} catch(ComposerException e) {
				logger.error(e.getErrorMsg() + ":" + e.toString() + ":" + e.getErrorMsg() + ":" + e.getExthr() + ":" + e.getExthr().getCause());
				return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE, MessageConstants.ERROR)).build();
		} catch(Exception e) {
				logger.error(e.toString() + ":" + e.getMessage() + ":" + e.getCause());
				return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE, MessageConstants.ERROR)).build();
		}
		return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE,
				MessageConstants.SUCCESS)).build();
	}

	@Path("/deleteSelectedTeachers")
	@POST
	@Produces("application/json")
	public Response deleteSelectedTeachers(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		try {
			composer.deleteSelectedTeachers(user, whereClause);
		} catch(ComposerException e) {
				logger.error(e.getErrorMsg() + ":" + e.toString() + ":" + e.getErrorMsg() + ":" + e.getExthr() + ":" + e.getExthr().getCause());
				return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE, MessageConstants.ERROR)).build();
		} catch(Exception e) {
				logger.error(e.toString() + ":" + e.getMessage() + ":" + e.getCause());
				return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE, MessageConstants.ERROR)).build();
		}
		return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE,
				MessageConstants.SUCCESS)).build();
	}
	
	@Path("/insertModerator")
	@POST
	@Produces("application/json")
	public Response insertModerator(@QueryParam("user") String user,
			@QueryParam("fields") String whereClause,
			@Context HttpServletRequest request) throws Exception {
		
		HttpSession session = request.getSession(false);
		user =	(String)session.getAttribute(CommonConstants.USERNAME);
		String result = null;
		result = composer.insertModerator(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/getModerators")
	@POST
	@Produces("application/json")
	public Response getModerators(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		String result = null;
		result = composer.getModerators(user, whereClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/updateModerator")
	@POST
	@Produces("application/json")
	public Response updateModerator(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause,
			@QueryParam("updateClause") String updateClause,
			@Context HttpServletRequest request) throws Exception {
		
		HttpSession session = request.getSession(false);
		user =	(String)session.getAttribute(CommonConstants.USERNAME);
		String result = null;
		result = composer.updateModerator(user, whereClause,
				updateClause);
		System.out.println("result : " + result);
		return Response.status(200).entity(result).build();
	}

	@Path("/deleteModerator")
	@POST
	@Produces("application/json")
	public Response deleteModerator(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		try {
			composer.deleteModerator(user, whereClause);
		} catch(ComposerException e) {
				logger.error(e.getErrorMsg() + ":" + e.toString() + ":" + e.getErrorMsg() + ":" + e.getExthr() + ":" + e.getExthr().getCause());
				return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE, MessageConstants.ERROR)).build();
		} catch(Exception e) {
				logger.error(e.toString() + ":" + e.getMessage() + ":" + e.getCause());
				return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE, MessageConstants.ERROR)).build();
		}
		return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE,
				MessageConstants.SUCCESS)).build();
	}

	@Path("/deleteSelectedModerators")
	@POST
	@Produces("application/json")
	public Response deleteSelectedModerators(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		try {
			composer.deleteSelectedModerators(user, whereClause);
		} catch(ComposerException e) {
				logger.error(e.getErrorMsg() + ":" + e.toString() + ":" + e.getErrorMsg() + ":" + e.getExthr() + ":" + e.getExthr().getCause());
				return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE, MessageConstants.ERROR)).build();
		} catch(Exception e) {
				logger.error(e.toString() + ":" + e.getMessage() + ":" + e.getCause());
				return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE, MessageConstants.ERROR)).build();
		}
		return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE,
				MessageConstants.SUCCESS)).build();
	}
	
	@Path("/classSectionDetails")
	@POST
	@Produces("application/json")
	public Response classSectionDetails(@QueryParam("user") String user,
			@QueryParam("whereClause") String whereClause) throws Exception {
		String result = null;
		try {
			System.out.println("here");
//		result = 	composer.classSectionDetails(user, whereClause);
		} catch(Exception e) {
				logger.error(e.toString() + ":" + e.getMessage() + ":" + e.getCause());
				return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.DELETE, MessageConstants.ERROR)).build();
		}
		return Response.status(200).entity(result).build();
	}
	
	@Path("/insertClassSectionDetails") 
	@POST
	@Produces("application/json")
	public Response insertClassSectionDetails(@QueryParam("user") String user,
							   @QueryParam("fields") String insertValues)
							throws Exception{			
//		try {
////			composer.insertClassSectionDetails(user, insertValues);
//		}
//		catch(ComposerException e) {
//			logger.error(e.getErrorMsg() + ":" + e.toString() + ":" + e.getErrorMsg() + ":" + e.getExthr() + ":" + e.getExthr().getCause());
//			return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.CREATE, MessageConstants.ALREADY_EXISTS)).build();
//		} catch(Exception e) {
//			logger.error(e.toString() + ":" + e.getMessage() + ":" + e.getCause());
//			return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.CREATE, MessageConstants.ERROR)).build();
//		}
		return Response.status(MessageConstants.STATUS_ACCEPTED).entity(UtilFunctions.createJsonObject(MessageConstants.CREATE,MessageConstants.SUCCESS)).build();
	}
}
